/*
 * @lc app=leetcode.cn id=1221 lang=java
 *
 * [1221] 分割平衡字符串
 */

// @lc code=start
class BalancedStringSplit {
  public int balancedStringSplit(String s) {
    char[] cs = s.toCharArray();
    int n = cs.length;
    int l = 0, r = 0, res = 0;
    for (int i = 0; i < n; i++) {
      if (cs[i] == 'L') {
        l++;
      } else {
        r++;
      }
      if (l == r && l != 0) {
        res++;
        l = 0;
        r = 0;
      }
    }
    return res;
  }
}
// @lc code=end
